#!/usr/bin/env python

from staticpage_class import StaticPage


def get_html_page ( liwe, page ):
	"""
	:INPUT:
		- page:              SAFE_STRING
	"""

	sp = StaticPage ( liwe )
	p = sp.get_html_page ( page )

	p = sp.templates [ 'HP_BOX_DEFAULT' ] % { '_txt': p }

	return { 'page': p }

def get_static_page_by_name ( liwe, page ):
	"""
	:INPUT:
		- page:              SAFE_STRING
	"""

	sp = StaticPage ( liwe )
	#p = sp.get_static_page_by_name ( page )
	#txt = ''
	#if p: txt = p [ 'html' ]

	#p = sp.templates [ 'HP_BOX_DEFAULT' ] % { '_txt': txt }

	return { 'page': sp.get_static_page_by_name ( page ) }

def search ( liwe, testo = '', _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:

		- testo:		SAFE_STRING
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	limit = _X_LINES

	s = StaticPage ( liwe )
	res = s.search ( testo = testo, start_pos = start_point, limit = limit )

	return _create_dataset ( start_point, _X_LINES, res [ 'rows' ], res [ 'tot_rows' ] )

def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1

	res [ 'rows' ] = tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = lines

	return res

def get_templates ( liwe ):
	"""
        :INPUT:
	"""
	sp = StaticPage ( liwe )
	return { "templates" : sp.get_templates () }
